package com.system.goodsserver.bus;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.system.goodsserver.bean.OrderInfo;
import com.system.goodsserver.service.GoodsService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

@Component
@RabbitListener(queues = "orderserver4")
public class TestReceiver {

    @Autowired
    private GoodsService goodsService;

    @RabbitHandler
    public void process(String msg){
        System.out.println("公共服务收到来自用户服务的消息: "+msg);
        Gson gson = new Gson();
        OrderInfo orderInfo = gson.fromJson(msg, OrderInfo.class);


        List<OrderInfo.OrderInfoBean.ListBean> list = orderInfo.getOrderInfo().getList();

        for (OrderInfo.OrderInfoBean.ListBean ls:list) {
            Integer goodsId=ls.getGoodsId();
            Integer salesGoodsNumber=ls.getGoodsNumber();
            //修改商品对应的销量
            goodsService.updateGoodsSalesNumber(goodsId,salesGoodsNumber);
        }


    }

}
